﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CommonExtensions.StringExtensions
{
    public static class StringExtensions
    {
        public static bool IsBlank(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static string Capitalize(this string str)
        {
            if(str.IsBlank())
                return str;

            var builder = new StringBuilder();
            builder.Append(char.ToUpper(str[0]));
            builder.Append(str.Substring(1, str.Length - 1));
            return builder.ToString();
        }

        public static string Humanize(this string str)
        {
            if (str.IsBlank())
                return str;

            Func<Match, string> matchCombiner = match => string.Format("{0} {1}", match.Groups[1],
                                                                       match.Groups[2]);

            return Regex.Replace(
                Regex.Replace(str, "([A-Z]+)([A-Z][a-z])", match => matchCombiner(match)),
                @"([a-z\d])([A-Z])", match => matchCombiner(match)).Capitalize();
        }

        public static string Titleize(this string str)
        {
            if (str.IsBlank())
                return str;

            return Regex.Replace(str.Humanize(), @"\w+", match => match.ToString().Capitalize());
        }
    }
}